﻿using Assets.BattleSystem.EnemyConfigs;
using Assets.Script.Data.EnemyData;
using Newtonsoft.Json;
using SimpleTools;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class SpawnCaseConfigWindow : EditorWindow
{
    public Transform Root;
    public string tCaseNum="01";
    public float intervalTime;
    public float moveSpeed;
    public float margin;



    [MenuItem("Tools/SpawnCaseConfig")]
    static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(SpawnCaseConfigWindow));
    }

    void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        GUILayout.BeginVertical();

        GUILayout.Space(10);
        GUI.skin.label.fontSize = 24;
        GUI.skin.label.alignment = TextAnchor.MiddleCenter;
        GUILayout.Label("MakeCaseConfig");

        GUILayout.Space(10);
        var caseNumF = EditorGUILayout.TextField("Case Num", tCaseNum);


        GUILayout.Space(10);
        GUI.skin.label.fontSize = 12;
        GUI.skin.label.alignment = TextAnchor.UpperLeft;
        var intervalTimeText = EditorGUILayout.FloatField("intervalTime", 0.015f);
        intervalTime = intervalTimeText;

        GUILayout.Space(10);
        GUI.skin.label.fontSize = 12;
        GUI.skin.label.alignment = TextAnchor.UpperLeft;
        var ttMoveSpeed = EditorGUILayout.FloatField("MoveSpeed", 5);
        moveSpeed = ttMoveSpeed;

        GUILayout.Space(10);
        GUI.skin.label.fontSize = 12;
        GUI.skin.label.alignment = TextAnchor.UpperLeft;
        var ttMargin = EditorGUILayout.FloatField("Margin", 10f);
        margin = ttMargin;

        GUILayout.Space(10);
        Root = (Transform)EditorGUILayout.ObjectField("CaseRoot",Root,typeof(Transform),true);

        GUILayout.EndHorizontal();

        EditorGUILayout.Space();
        if (GUILayout.Button("Make Case Config"))
        {
            SpawnCaseConfig_();
        }

        GUILayout.EndVertical();
    }



    public void SpawnCaseConfig_()
    {
        CaseSpawnConfig spawnConfig = new CaseSpawnConfig();
        spawnConfig.caseName = "Case"+tCaseNum;

        int id = 0;
        Dictionary<EnemyData.EnemyDataStuct,int> tecDic = new Dictionary<EnemyData.EnemyDataStuct, int>();

        foreach (var item in tecDic)
        {
            Debug.Log(item.Value);
        }

        for (int i = 0; i < Root.childCount; i++)
        {
            var ct = Root.GetChild(i);
            for (int j = 0; j < ct.childCount; j++)
            {
                var ecc = ct.GetChild(j).GetComponent<EnemyConfigCOM>();
                if (ecc != null)
                {
                    if (!tecDic.ContainsKey(ecc.form))
                    {
                        id++;
                        ecc.EnemyConfigID = id;
                        tecDic.Add(ecc.form, ecc.EnemyConfigID);
                    }
                    else
                    {
                        ecc.EnemyConfigID = tecDic[ecc.form];
                    }
                }
            }
            
        }



        //倒置tecDic
        foreach (var item in tecDic)
        {
            spawnConfig.enemyConfigDic.Add(item.Value, new EnemyData(item.Key) { perfabPath = item.Key.perfabPath });
        }

        //生成<帧数，出生信息List>
        int nId = 0;
        Dictionary<int, List<EnemySpawnConfig>> enemySpawnDic = new Dictionary<int, List<EnemySpawnConfig>>();
        for (int i = 0; i < Root.childCount; i++)
        {
            var ct = Root.GetChild(i);
            int fTime = (int)((ct.transform.position.x - margin) / moveSpeed / intervalTime);
            if (!enemySpawnDic.ContainsKey(fTime))
            {
                enemySpawnDic.Add(fTime, new List<EnemySpawnConfig>());
            }

            for (int j = 0; j < ct.childCount; j++)
            {
                var ecc = ct.GetChild(j).GetComponent<EnemyConfigCOM>();
                if (ecc != null)
                {
                    var t = new EnemySpawnConfig() { cId = ecc.EnemyConfigID, netId = nId++, dieFrame = fTime+(int)(ecc.form.autoDieTime/intervalTime) };
                    t.x = margin + ct.position.x + ecc.transform.localPosition.x;
                    t.y = ecc.transform.position.y;
                    t.z = ecc.transform.position.z;
                    enemySpawnDic[fTime].Add(t);
                }
            }
        }
        //生成List<出生信息>
        List<EnemySpawnInfo> esiList = new List<EnemySpawnInfo>();
        foreach (var item in enemySpawnDic)
        {
            
            esiList.Add(new EnemySpawnInfo() { spawnFrame = item.Key, enemyConfigs = item.Value.ToArray() });
        }
        //生成ESInfo[]
        spawnConfig.enemySpawnInfos = esiList.OrderBy(i => i.spawnFrame).ToArray();

        FileHelp.SaveStringData("Configs/Case/Case" + tCaseNum + ".json", JsonConvert.SerializeObject(spawnConfig));
        Debug.Log("生成成功");
    }
}
